package com.ma.cardgame

import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class ThirdActivity : AppCompatActivity() {
    private lateinit var imgAvatarEdit: ImageView
    private lateinit var etUsername: EditText
    private lateinit var etGender: EditText
    private lateinit var etAccount: EditText
    private lateinit var etPhone: EditText
    private lateinit var etEmail: EditText
    private lateinit var etHobby: EditText

    private var selectedAvatarUri: Uri? = null

    private val pickImageLauncher = registerForActivityResult(
        ActivityResultContracts.GetContent()
    ) { uri: Uri? ->
        if (uri != null) {
            selectedAvatarUri = uri
            imgAvatarEdit.setImageURI(uri)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_third)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        imgAvatarEdit = findViewById(R.id.img_avatar_edit)
        etUsername = findViewById(R.id.et_username)
        etGender = findViewById(R.id.et_gender)
        etAccount = findViewById(R.id.et_account)
        etPhone = findViewById(R.id.et_phone)
        etEmail = findViewById(R.id.et_email)
        etHobby = findViewById(R.id.et_hobby)

        // 加载已保存资料作为初始值
        val sp = getSharedPreferences("profile", MODE_PRIVATE)
        etUsername.setText(sp.getString("username", ""))
        etGender.setText(sp.getString("gender", ""))
        etAccount.setText(sp.getString("account", ""))
        etPhone.setText(sp.getString("phone", ""))
        etEmail.setText(sp.getString("email", ""))
        etHobby.setText(sp.getString("hobby", ""))
        sp.getString("avatar_uri", null)?.let { uriStr ->
            try {
                val uri = Uri.parse(uriStr)
                selectedAvatarUri = uri
                imgAvatarEdit.setImageURI(uri)
            } catch (_: Exception) {
                // 如果URI无效，使用默认头像
                imgAvatarEdit.setImageResource(R.drawable.card)
            }
        } ?: run {
            // 没有保存的头像，使用默认头像
            imgAvatarEdit.setImageResource(R.drawable.card)
        }

        // 选择头像
        findViewById<Button>(R.id.btn_pick_avatar).setOnClickListener {
            pickImageLauncher.launch("image/*")
        }

        // 保存
        findViewById<Button>(R.id.btn_save).setOnClickListener {
            sp.edit()
                .putString("username", etUsername.text.toString())
                .putString("gender", etGender.text.toString())
                .putString("account", etAccount.text.toString())
                .putString("phone", etPhone.text.toString())
                .putString("email", etEmail.text.toString())
                .putString("hobby", etHobby.text.toString())
                .apply()

            selectedAvatarUri?.let { uri ->
                sp.edit().putString("avatar_uri", uri.toString()).apply()
            }

            finish()
        }

        // 取消
        findViewById<Button>(R.id.btn_cancel).setOnClickListener {
            finish()
        }
    }


}